const app = getApp()
import{getUrl}from "../../utils/url";
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:1,
    load:false,
    size:10,
    tag:['校园帮','我发出的','我帮助的'],
    flag:0,
    url:'get',
    wheres:"",
    bt:0,
    sorts:"",
    fields:'',
    wx_id:wx.getStorageSync("user").id
  },
  getWxsmData(){
    let date = new Date()
    let m = date.getMonth() + 1
    let month = m < 10 ? ""+"0"+m:m
    let com = date.getFullYear() + '-' + month+'%'
    app.com.post('anlysis/get/wx/sm',{wx_id:wx.getStorageSync("user").id,com_time:com},function(res){
      console.log(res)
      if(res.code == 1){
        _this.setData({
          anlysis:res.data
        })
      }
    })
  },
  comfirm(e){
    let id = e.currentTarget.dataset.id
    wx.showLoading({
      title: '请稍等',
      task:true
    })
    app.com.post('help/confirm',{id:id},function(res){
      wx.hideLoading()
      if(res.code == 1){
        wx.showToast({
          title: '订单已完成',
        })
        _this.getList(0)
      }else{
        wx.showToast({
          title: '确认失败',
          icon: 'none'
        })
      }
    })
  },
  takeIt(e){
    let index = e.currentTarget.dataset.index
    let msg = this.data.list[index]
    if (wx.getStorageSync("res").state == 1){
      if (wx.getStorageSync("res").a_id == wx.getStorageSync("area").pk_id) {
        this.takeDo(msg)
      }else{
        wx.showModal({
          title: '提示',
          content: '您不是该学校的接单员',
          confirmText: '朕知道了',
          showCancel:false,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/register/register',
              })
            }
          }
        })
      }
    } else{
      wx.showModal({
        title: '提示',
        content: '您还不是接单员，是否前往申请',
        confirmText:'立即前往',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/register/register',
            })
          }
        }
      })
    }
    
    
  },
  pay(e) {
    wx.showLoading({
      title: '请稍等',
      task: true
    })
 },
  cancel(e){
    wx.showModal({
      title: '提示',
      content: '确定要取消吗？',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '请稍等',
            task: true
          })
          app.com.cancel(e.currentTarget.dataset.id, 'navigateTo',function(res){
            wx.hideLoading()
            if(res){
              _this.getList(0)
            }
          })
        }
      }
    })
    
  },
  changeTag(e) {
    let index = e.currentTarget.dataset.index
    let bt=0;
    this.setData({
      flag: e.currentTarget.dataset.index
    })
    if(index == 0){
     bt=1;
    let orders={
      schoolId:wx.getStorageSync('area').id,//学校id   
    }
     _this.getList(orders);
      
    }else if(index == 1){
      bt=2;
      let orders={
        userId:wx.getStorageSync('user').id,//我发出的   
      }
       _this.getList(orders);
    }else{
      bt=3;
      let orders={
        helpId:wx.getStorageSync('user').id,//我帮助的 
      }
       _this.getList(orders);
    }
     this.setData({bt});
  },
  navTo(e) {



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
  },
 
  getList(orders){
    this.setData({
      list:[]
    })
      let {page}=this.data;
      let {list}=this.data;
      wx.showLoading({
        title: '加载中',
      })
    wx.request({
      url: getUrl()+`/orderS/findBy?page=${page}`,  //路径
      data:orders,page,//参数
      success: function(res) {
        wx.hideLoading();
         //数组 的拼接
         _this.setData({
           list:[...list,...res.data]
         })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('area').id===undefined||wx.getStorageSync('area').id==''){
            wx.switchTab({
              url: '../index/index',
            })
            wx.showToast({
              title: '请先选择学校',
              icon:"none"
            })
    }else{
        
    this.showList();
    this.setData({
      wx_id: wx.getStorageSync("user").id
    })  
    }

  },

  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.showList();
    this.setData({
      page:1
    })  

  
  },
  onReachBottom(){
    //判断有不有下有一页数据
     console.log("ee");
},

/**
 *  显示数据
 */
    showList(){
    const {flag} = this.data;
      _this.setData({
        list:[]
      })
        let bt=  this.data.bt;
     if(flag+1==1){
      let orders={
        schoolId:wx.getStorageSync('area').id,//学校id   
      }
       _this.getList(orders);
     }else if(flag+1==2){
      let orders={
        userId:wx.getStorageSync('user').id,//我发出的   
      }
       _this.getList(orders);
     }else if(flag+1==3){
      let orders={
        helpId:wx.getStorageSync('user').id,//我帮助的 
      }
       _this.getList(orders);
     }else{
      let orders={
        userId:wx.getStorageSync('user').id,//我发出的   
      }
      _this.getList(orders);
     }

    },

  /**
   * 页面上拉触底事件的处理函数
   * 
   * 
   */
    onReachBottom: function() {
      // 页面触底时执行
       let {page}=this.data;
       page++;
       this.setData({
         page
       })
       this.getList();
    },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:'校园跑腿',
      path:'/pages/index/index'
    }
  }
})